package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.*;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("goddsController")
public class GoddsController {
    @Reference
    private ItemCatService itemCatService;
    @Reference
    private TemplateService templateService;
    @Reference
    private TbGoodsDescService tbGoodsDescService;
    @Reference
    private TbGoodsService tbGoodsService;
    @Reference
    private SpecificationOptionService specificationOptionService;
    @Reference
    private TbItemService tbItemService;
    //跳转增加商品页面
    @RequestMapping("toGoddsedit")
    public String toGoodsEdit(){
        return "admin/goods_edit";
    }

    //查询所有分类管理（分级）
    @RequestMapping("findAll")
    @ResponseBody
    public StatusCode findAll(@RequestParam(defaultValue = "0") Long id){
        List<TbItemCat> list = itemCatService.findAll(id);
        if (list == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(list);
    }

    //查询商品分类所对应的品牌
    @RequestMapping("selectTemplate")
    @ResponseBody
    public StatusCode selectTemplate(Long id){
        if (id == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        TbTypeTemplate tbTypeTemplate = templateService.selectById(id);
        if (tbTypeTemplate == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(tbTypeTemplate);
    }

    //获取typeid
    @RequestMapping("selectTypeId")
    @ResponseBody
    public StatusCode selectTypeId(Long id){
        if (id == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        TbItemCat tbItemCat = itemCatService.selectTypeId(id);
        if (tbItemCat == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(tbItemCat);
    }

    //商品增加
    @RequestMapping("insertGoods")
    @ResponseBody
    public StatusCode insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,String itemJson,String itemJson2,String itemJson3){
        List<TbItem> tbItems1 = JSONObject.parseArray(itemJson, TbItem.class);
        List<TbItem> tbItems = JSONObject.parseArray(itemJson2,TbItem.class);
        List<TbItem> tbItems2 = JSONObject.parseArray(itemJson3,TbItem.class);
        //获取shiro域对象中的用户
        TbSeller tbSeller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        tbGoods.setSellerId(tbSeller.getSellerId());
        String sellerId = tbSeller.getSellerId();
        Boolean b = tbGoodsService.insertTbGoodsAndDesc(tbGoods,tbGoodsDesc,tbItems,tbItems1.get(0),sellerId,tbItems2);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //跳转图片增加模态框
    @RequestMapping("toinsertimg")
    public String toInsertImg(){
        return "admin/insert_goodsimage";
    }


    //查询拼接规格
    @RequestMapping("getitems")
    @ResponseBody
    public List<TbSpecAndOption> getItems(Long id){
        //根据typeid查询模板
        TbTypeTemplate tbTypeTemplate = templateService.selectById(id);
        String specIds = tbTypeTemplate.getSpecIds();
        List<TbSpecification> tbSpecifications = JSONObject.parseArray(specIds, TbSpecification.class);
        List<TbSpecAndOption> list = new ArrayList<>();
        for (int i = 0; i < tbSpecifications.size(); i++) {
            Long id1 = tbSpecifications.get(i).getId();
            List<TbSpecificationOption> Options = specificationOptionService.selectOptionBySpecid(id1);
            TbSpecAndOption vo = new TbSpecAndOption();
            vo.setTbSpecification(tbSpecifications.get(i));
            vo.setTbSpecificationOptions(Options);
            list.add(vo);
        }

        return list;
    }

    //跳转商品管理
    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }

    //商家后台查询所有商品
    @RequestMapping("selectItem")
    @ResponseBody
    public StatusCode selectItem(String auditStatus,String goodsName,@RequestParam(defaultValue = "1")Integer pageNum,
                                 @RequestParam(defaultValue = "5")Integer pageSize){
        PageUtil page = tbItemService.findallItem2(auditStatus,goodsName,pageNum,pageSize);
        return StatusCode.build(ConstantEnum.SUCCESS).setT(page);
    }

    //删除商品
    @RequestMapping("deleteGoods")
    @ResponseBody
    public StatusCode deleteGoods(Long[] ids){
        Boolean b = tbGoodsService.deleteGoods(ids);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.ERROR);
    }

    //提交审核
    @RequestMapping("statusUpdate")
    @ResponseBody
    public StatusCode statusUpdate(Long[] ids){
        Boolean b = tbGoodsService.statusUpdate(ids);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.ERROR);
    }
}
